package com.mofofa.maozy.me

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.base.BaseFragment
import com.mofofa.maozy.components.dialog.CustomerServiceDialog
import com.mofofa.maozy.components.dialog.MessageDialog
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.databinding.FragmentMaozyMeBinding
import com.mofofa.maozy.extend.startMaozyToMaozyLoginMaozyActivity
import com.mofofa.maozy.extend.startMaozyToMaozyNextMaozyActivity
import com.mofofa.maozy.extend.toMaozyEncryptMaozyPhoneMaozyNumber
import com.mofofa.maozy.utils.UsageCountryUtil
import com.mofofa.maozy.utils.UserStateUtil


class MeRepayFragment : BaseFragment() {

    private lateinit var mMaozyFragmentMaozyBinding: FragmentMaozyMeBinding
    private lateinit var mCustomerServiceAdapter: CustomerServiceAdapter

    val mMeViewModel by lazy {
        ViewModelProvider(this).get(MeViewModel::class.java)
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mMeViewModel)
        return list
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        mMaozyFragmentMaozyBinding =
            FragmentMaozyMeBinding.inflate(inflater, container, false)
        return mMaozyFragmentMaozyBinding.root
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        try {
            //初始化客服列表
            mCustomerServiceAdapter =
                CustomerServiceAdapter(mMeViewModel.mMaozyCustomerMaozyServiceMaozyList)
            mMaozyFragmentMaozyBinding.rcMeCustomerServiceList.layoutManager =
                LinearLayoutManager(requireContext())
            mMaozyFragmentMaozyBinding.rcMeCustomerServiceList.adapter =
                mCustomerServiceAdapter

            //获取app配置回调
            mMeViewModel.onMaozyFetchMaozyContactMaozyListMaozyFromMaozyCachedMaozyLiveData.observe(
                viewLifecycleOwner
            ) {
                mCustomerServiceAdapter.notifyDataSetChanged()
            }

            //获取app配置回调
            mMeViewModel.onMaozyFetchMaozyContactMaozyListMaozyLiveData.observe(
                viewLifecycleOwner
            ) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        mMeViewModel.mMaozyCustomerMaozyServiceMaozyList.addAll(
                            result
                        )
                        mCustomerServiceAdapter.notifyDataSetChanged()
                    }
                }
            }

            //点击反馈
            mMaozyFragmentMaozyBinding.rlMeFeedbackBg.setOnClickListener {
                (requireContext() as BaseActivity).startMaozyToMaozyNextMaozyActivity<FeedbackActivity>()
            }

            //点击退出登录
            mMaozyFragmentMaozyBinding.submitMeLogout.setOnClickListener {
                MessageDialog.showMaozyDialog(
                    (requireContext() as BaseActivity).supportFragmentManager,
                    getString(R.string.maozy_are_u_sure_to_log_out_of_the_app),
                    getString(R.string.maozy_no),
                    getString(R.string.maozy_yes)
                ) {
                    UserStateUtil.logoutMaozy()
                    startMaozyToMaozyLoginMaozyActivity()
                }
            }

            //初始化姓名
            mMaozyFragmentMaozyBinding.textMeUserName.text =
                UserStateUtil.getMaozyUserMaozyName()

            //初始化电话
            mMaozyFragmentMaozyBinding.textMeUserMobile.text =
                UserStateUtil.mMaozyMobile.toMaozyEncryptMaozyPhoneMaozyNumber()

            //点击客服
            mMaozyFragmentMaozyBinding.rlMeCustomerServiceBg.setOnClickListener {
                CustomerServiceDialog.showMaozyDialog(requireActivity().supportFragmentManager)
            }

            //更新页面显示
            updateMaozyCountryMaozyDisplayMaozyText()

            //获取app配置
            mMeViewModel.fetchMaozyContactMaozyList(
                ReqBase(
                    showLoading = false
                )
            )

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun updateMaozyCountryMaozyDisplayMaozyText() {
        if (UsageCountryUtil.isMaozyCountryMaozyCotediva()) {
            mMaozyFragmentMaozyBinding.textMeServiceFeedback.text =
                getString(R.string.maozy_service_feedback_fcfa)
            mMaozyFragmentMaozyBinding.textMeCustomerService.text =
                getString(R.string.maozy_customer_service_fcfa)
            mMaozyFragmentMaozyBinding.textMeCustomerServiceRemindTitle.text =
                getString(R.string.maozy_customer_service_tips_fcfa)
            mMaozyFragmentMaozyBinding.submitMeLogout.setMaozyTitle(getString(R.string.maozy_logout_fcfa))
        }
    }
}